package controler;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.HoadonModel;
import entity.Hoadon;

/**
 * Servlet implementation class admintimorder
 */
@WebServlet("/admintimorder")
public class admintimorder extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public admintimorder() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @throws SQLException 
	 * @throws IOException 
	 * @throws ServletException 
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
    public void loadorder(HttpServletRequest request, HttpServletResponse response){
    	HttpSession session = request.getSession();
    	HoadonModel model = new HoadonModel();
    	List<Hoadon> listallhd = new ArrayList<Hoadon>();
    	String strSearch ="select HOTEN, HOADON.MAHD , TENSP,CTHD.SOLUONG,CTHD.DONGIA,HOADON.TRANGTHAI from HOADON,CTHD,SANPHAM,KHACHHANG where HOADON.MAHD= CTHD.MAHD and CTHD.MASP = SANPHAM.MASP and KHACHHANG.MAKH = HOADON.MAKH";
    	try {
			listallhd = model.laycthd(strSearch);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	session.setAttribute("listallhd", listallhd);
    }
  
    protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws SQLException, ServletException, IOException{
    	response.setContentType("text/html;charset=UTF-8");
    	HttpSession session = request.getSession();
    	HoadonModel model = new HoadonModel();
    	List<Hoadon> listhd = new ArrayList<Hoadon>();
    	String strSearch ="select HOTEN, HOADON.MAHD , TENSP,CTHD.SOLUONG,CTHD.DONGIA,HOADON.TRANGTHAI from HOADON,CTHD,SANPHAM,KHACHHANG where HOADON.MAHD= CTHD.MAHD and CTHD.MASP = SANPHAM.MASP and KHACHHANG.MAKH = HOADON.MAKH and CTHD.MAHD like '"+request.getParameter("mahd")+"'";
    	listhd = model.laycthd(strSearch);
    	session.setAttribute("listhd", listhd);
    	RequestDispatcher view = request.getRequestDispatcher("adminxemorder.jsp");
        view.forward(request, response);
    }
    
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		try {
			processRequest(request, response);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		try {
			processRequest(request, response);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
